package gdt.entity;

import gdt.entity.AbstractSecureEntity;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.*;

import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.NotNull;

import static javax.persistence.FetchType.LAZY;
import static javax.persistence.CascadeType.ALL;

/**
 * Entity implementation class for Entity: Project
 *
 */
@NamedQuery(name="findProjectByGroupAndName",query="select p from Project p where p.group = :group and p.name = :name")
@Entity
public class Project extends AbstractSecureEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	@NotEmpty
	private String name;
	
	private String description;
	
	private Integer hours;
	
	@NotNull
	private Boolean template = false;
	
	@NotNull
	private Boolean retired = false;
	
	@NotNull
	@ManyToOne(fetch=LAZY)
	private Group group;

	@ManyToOne(fetch=LAZY)
	private Client client;
	
	@OneToMany(fetch=LAZY, mappedBy = "parentProject", cascade = ALL)
	private List<Category> categories = new LinkedList<Category>();
	
	@ManyToMany(fetch=LAZY)
	private List<Member> assignedMembers = new LinkedList<Member>();
	
	@OneToMany(fetch=LAZY, mappedBy = "project", cascade = ALL)
	private List<ProjectAnnouncement> announcements = new LinkedList<ProjectAnnouncement>();
	
	
	// ********************** Accessor Methods ********************** //
	
	public Project() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getHours() {
		return hours;
	}

	public void setHours(Integer hours) {
		this.hours = hours;
	}

	public Boolean isTemplate() {
		return template;
	}

	public void setTemplate(Boolean template) {
		this.template = template;
	}

	public Boolean getRetired() {
		return retired;
	}

	public void setRetired(Boolean retired) {
		this.retired = retired;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public Group getGroup() {
		return group;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public Client getClient() {
		return client;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setAssignedMembers(List<Member> assignedMembers) {
		this.assignedMembers = assignedMembers;
	}

	public List<Member> getAssignedMembers() {
		return assignedMembers;
	}

	public void setAnnouncements(List<ProjectAnnouncement> announcements) {
		this.announcements = announcements;
	}

	public List<ProjectAnnouncement> getAnnouncements() {
		return announcements;
	}
	
	public long getMinutesWorked(){
		long minutes = 0;
		for(Category category : categories){
			minutes += category.getMinutesWorked();
		}
		return minutes;
	}
	
	
	public List<Task> getAllTasks(){
		List<Task> tasks = new LinkedList<Task>();
		for(Category c : getCategories()){
			tasks.addAll(c.getAllTasks());
		}
		return tasks;
	}
	
	
	// *********************** Adder Methods *********************** //
	
	public void addMember(Member member){
		this.assignedMembers.add(member);
		member.getAssignedProjects().add(this);
	}
	
	public void addCategory(Category category){
		this.categories.add(category);
		category.setParentProject(this);
	}
	
	public void addAnnouncement(ProjectAnnouncement announcement){
		this.announcements.add(announcement);
		announcement.setProject(this);
	}
   
	// *********************** Remover Methods *********************** //
	
	public void removeMember(Member member){
		this.assignedMembers.remove(member);
	}
	
	public void removeCategory(Category category){
		this.categories.remove(category);
	}
	
	public void removeAnnouncement(ProjectAnnouncement announcement){
		this.announcements.remove(announcement);
	}
}
